﻿/****---------------------------------------------------------------------------------------------------->>>>
*	LoadController for Flash 8 [v1.0]
*	Coded to work with ActionScript 2.0
*	--------------------------------------------------
*	Created by
*		Lance Den (vistaden@msn.com , yanjun@vika.cn)
*		SIAN Digital Technology co.,ltd. (www.vika.cn)
*
*	Based on code by
*		(null)
*
*	Version History:
*		2007-09-10 - Estabished by Lance Den.
*
*	Authors notes:
*		Class Initialization:
*			- null
*
*			Example:
*				null
*
*			Note:
*				null
*
*			Example:
*				null
*
*		- Lance Den 2007-09-10
<<<<----------------------------------------------------------------------------------------------------****/

import MovieClipLoader;
import mx.utils.Delegate;
import mx.events.EventDispatcher;

//[Event("onLoadComplete")]
//[Event("onLoadError")]
//[Event("onLoadInit")]
//[Event("onLoadProgress")]
//[Event("onLoadStart")]

class mx.utils.LoadController
{
	/*
	[url, ...]
	*/
	private var ary_cache   : Array;
	/*
	[{url, target}, ...]
	*/
	private var ary_load    : Array;
	
	private var mcl_loader  : MovieClipLoader;
	private var lsn_forload : Object;
	
	// cache file container
	private var mc_cache : MovieClip;
	// cache or load file list array index
	private var num_lid  : Number;
	// the tag denote load or cache
	private var str_tag  : String;
	
	// init EventDispatcher
	public  var addEventListener:Function;
	public  var removeEventListener:Function;
	private var dispatchEvent:Function;
	private var dispatchQueue:Function;
	private static var _mixin1 = EventDispatcher.initialize(LoadController.prototype);
	
	/*------------------------------------------------------------------------>
	* Class Constructor
	<------------------------------------------------------------------------*/
	function LoadController()
	{
		this.initClass();
	}
	
	/*------------------------------------------------------------------------>
	* init
	<------------------------------------------------------------------------*/
	private function initClass()
	{
		this.lsn_forload = new Object();
		this.lsn_forload.onLoadComplete = Delegate.create(this, onLoadComplete);
		this.lsn_forload.onLoadError    = Delegate.create(this, onLoadError);
		this.lsn_forload.onLoadInit     = Delegate.create(this, onLoadInit);
		this.lsn_forload.onLoadProgress = Delegate.create(this, onLoadProgress);
		this.lsn_forload.onLoadStart    = Delegate.create(this, onLoadStart);
		
		this.create();
	}
	
	/*
	public function initialize(){}
	*/
	
	/*------------------------------------------------------------------------>
	* public
	<------------------------------------------------------------------------*/
	// create
	public function create():Void
	{
		if(this.mcl_loader == undefined)
		{
			this.mcl_loader = new MovieClipLoader();
			this.mcl_loader.addListener(this.lsn_forload);
		}
	}
	
	/*------------------------------------------------------------------------>
	* private -- load monitor
	<------------------------------------------------------------------------*/
	private function onLoadComplete(target:MovieClip):Void
	{
		this.num_lid++;
		
		if (this.str_tag == "load")
		{
			if(this.num_lid <= (this.ary_load.length-1))
			{
				this.mcl_loader.loadClip(this.ary_load[this.num_lid].url, this.ary_load[this.num_lid].target);
			}else
			{
				this.num_lid = 0;
			}
		}else if (this.str_tag == "cache")
		{
			this.unloadFile(this.mc_cache);
			if(this.num_lid <= (this.ary_cache.length-1))
			{
				this.mcl_loader.loadClip(this.ary_cache[this.num_lid], this.mc_cache);
			}else
			{
				this.num_lid = 0;
			}
		}
		
		if(this.num_lid == 0)
		{
			dispatchEvent({type:"onLoadComplete", target:target});
		}
	}
	
	private function onLoadError(target:MovieClip, errorCode:String):Void
	{
		dispatchEvent({type:"onLoadError", target:target, errorCode:errorCode});
	}
	
	private function onLoadProgress(target:MovieClip, bytesLoaded:Number, bytesTotal:Number):Void
	{
		dispatchEvent({type:"onLoadProgress", target:target, bytesLoaded:bytesLoaded, bytesTotal:bytesTotal});
	}
	
	private function onLoadStart(target:MovieClip):Void
	{
		var num_current:Number;
		var num_total:Number;
		
		num_current = this.num_lid+1;
		if(this.str_tag == "load")
		{
			num_total = this.ary_load.length;
		}else if (this.str_tag == "cache")
		{
			num_total = this.ary_cache.length;
		}
		dispatchEvent({type:"onLoadStart", target:target, current:num_current, total:num_total});
	}
	
	private function onLoadInit(target:MovieClip):Void
	{
		dispatchEvent({type:"onLoadInit", target:target});
	}
	
	
	/*------------------------------------------------------------------------>
	* load a file in some movieclip
	<------------------------------------------------------------------------*/
	public function loadFile(str_url:String, mc_target:MovieClip):Void
	{
		this.loadFiles([{url:str_url,target:mc_target}]);
	}
	
	public function loadFiles(ary_list:Array):Void
	{
		this.str_tag  = "load";
		this.ary_load = ary_list;
		this.num_lid  = 0;
		this.mcl_loader.loadClip(this.ary_load[0].url, this.ary_load[0].target);
	}
	
	/*------------------------------------------------------------------------>
	* cache a file
	<------------------------------------------------------------------------*/
	public function cacheFile(str_url:String, mc_target:MovieClip):Void
	{
		this.cacheFiles([str_url], mc_target);
	}
	
	public function cacheFiles(ary_list:Array, mc_target:MovieClip):Void
	{
		this.str_tag   = "cache";
		this.ary_cache = ary_list;
		this.mc_cache  = mc_target;
		this.num_lid   = 0;
		this.mcl_loader.loadClip(this.ary_cache[0], this.mc_cache);
	}
	
	/*------------------------------------------------------------------------>
	* unload a movie
	<------------------------------------------------------------------------*/
	public function unloadFile(mc_target:MovieClip)
	{
		this.mcl_loader.unloadClip(mc_target);
	}
	
	
}

